package com.buss.group.controller;
import com.buss.group.entity.BnsGroupArticleCommentEntity;
import com.buss.group.service.BnsGroupArticleCommentServiceI;

import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.context.annotation.Scope;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import java.io.OutputStream;

import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jeecgframework.core.util.ResourceUtil;

import java.io.IOException;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Map;

import jodd.io.http.Http;

import org.jeecgframework.core.util.ExceptionUtil;



/**   
 * @Title: Controller
 * @Description: 群组文章主题回复记录
 * @author onlineGenerator
 * @date 2016-09-06 19:51:26
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/bnsGroupArticleCommentController")
public class BnsGroupArticleCommentController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BnsGroupArticleCommentController.class);

	@Autowired
	private BnsGroupArticleCommentServiceI bnsGroupArticleCommentService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 群组文章主题回复记录列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/buss/group/bnsGroupArticleCommentList");
	}
	
	/**
	 * 群组文章主题回复评论列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "viewCommentWin")
	public ModelAndView viewCommentWin(HttpServletRequest request){
		String aid=request.getParameter("aid");
		request.setAttribute("aid", aid);
		return new ModelAndView("com/buss/group/showGroupArticleCommentListWin");
		
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(BnsGroupArticleCommentEntity bnsGroupArticleComment,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(BnsGroupArticleCommentEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, bnsGroupArticleComment, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		String aid=request.getParameter("aid");
		if(!StringUtil.isEmpty(aid))
			cq.eq("articleId", aid);
		cq.addOrder("createDate", SortDirection.desc);
		cq.add();
		this.bnsGroupArticleCommentService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
//
//	@RequestMapping(params = "datagrid_win")
//	//查看返回评论列表
//	public void datagrid_win(BnsGroupArticleCommentEntity bnsGroupArticleComment,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
//		CriteriaQuery cq = new CriteriaQuery(BnsGroupArticleCommentEntity.class, dataGrid);
//		//查询条件组装器
//		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, bnsGroupArticleComment, request.getParameterMap());
//		try{
//		//自定义追加查询条件
//		}catch (Exception e) {
//			throw new BusinessException(e.getMessage());
//		}
//		String aid=request.getParameter("aid");
//		if(!StringUtil.isEmpty(aid))
//			cq.eq("articleId", aid);
//		cq.add();
//		systemService.getDataGridReturn(cq, isOffset);
//		this.bnsGroupArticleCommentService.getDataGridReturn(cq, true);
//		TagUtil.datagrid(response, dataGrid);
//	}
	

	/**
	 * 删除群组文章主题回复记录
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(BnsGroupArticleCommentEntity bnsGroupArticleComment, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		bnsGroupArticleComment = systemService.getEntity(BnsGroupArticleCommentEntity.class, bnsGroupArticleComment.getId());
		message = "群组文章主题回复记录删除成功";
		try{
			bnsGroupArticleCommentService.delete(bnsGroupArticleComment);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "群组文章主题回复记录删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除群组文章主题回复记录
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "群组文章主题回复记录删除成功";
		try{
			for(String id:ids.split(",")){
				BnsGroupArticleCommentEntity bnsGroupArticleComment = systemService.getEntity(BnsGroupArticleCommentEntity.class, 
				id
				);
				bnsGroupArticleCommentService.delete(bnsGroupArticleComment);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "群组文章主题回复记录删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加群组文章主题回复记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(BnsGroupArticleCommentEntity bnsGroupArticleComment, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "群组文章主题回复记录添加成功";
		try{
			bnsGroupArticleCommentService.save(bnsGroupArticleComment);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "群组文章主题回复记录添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 更新群组文章主题回复记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(BnsGroupArticleCommentEntity bnsGroupArticleComment, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "群组文章主题回复记录更新成功";
		BnsGroupArticleCommentEntity t = bnsGroupArticleCommentService.get(BnsGroupArticleCommentEntity.class, bnsGroupArticleComment.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(bnsGroupArticleComment, t);
			bnsGroupArticleCommentService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "群组文章主题回复记录更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 群组文章主题回复记录新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(BnsGroupArticleCommentEntity bnsGroupArticleComment, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(bnsGroupArticleComment.getId())) {
			bnsGroupArticleComment = bnsGroupArticleCommentService.getEntity(BnsGroupArticleCommentEntity.class, bnsGroupArticleComment.getId());
			req.setAttribute("bnsGroupArticleCommentPage", bnsGroupArticleComment);
		}
		return new ModelAndView("com/buss/group/bnsGroupArticleComment-add");
	}
	/**
	 * 群组文章主题回复记录编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(BnsGroupArticleCommentEntity bnsGroupArticleComment, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(bnsGroupArticleComment.getId())) {
			bnsGroupArticleComment = bnsGroupArticleCommentService.getEntity(BnsGroupArticleCommentEntity.class, bnsGroupArticleComment.getId());
			req.setAttribute("bnsGroupArticleCommentPage", bnsGroupArticleComment);
		}
		return new ModelAndView("com/buss/group/bnsGroupArticleComment-update");
	}
	
	/**
	 * 导入功能跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name","bnsGroupArticleCommentController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}
	
	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(BnsGroupArticleCommentEntity bnsGroupArticleComment,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
		CriteriaQuery cq = new CriteriaQuery(BnsGroupArticleCommentEntity.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, bnsGroupArticleComment, request.getParameterMap());
		List<BnsGroupArticleCommentEntity> bnsGroupArticleComments = this.bnsGroupArticleCommentService.getListByCriteriaQuery(cq,false);
		modelMap.put(NormalExcelConstants.FILE_NAME,"群组文章主题回复记录");
		modelMap.put(NormalExcelConstants.CLASS,BnsGroupArticleCommentEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("群组文章主题回复记录列表", "导出人:"+ResourceUtil.getSessionUserName().getRealName(),
			"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST,bnsGroupArticleComments);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	/**
	 * 导出excel 使模板
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(BnsGroupArticleCommentEntity bnsGroupArticleComment,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
    	modelMap.put(NormalExcelConstants.FILE_NAME,"群组文章主题回复记录");
    	modelMap.put(NormalExcelConstants.CLASS,BnsGroupArticleCommentEntity.class);
    	modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("群组文章主题回复记录列表", "导出人:"+ResourceUtil.getSessionUserName().getRealName(),
    	"导出信息"));
    	modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
    	return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<BnsGroupArticleCommentEntity> listBnsGroupArticleCommentEntitys = ExcelImportUtil.importExcel(file.getInputStream(),BnsGroupArticleCommentEntity.class,params);
				for (BnsGroupArticleCommentEntity bnsGroupArticleComment : listBnsGroupArticleCommentEntitys) {
					bnsGroupArticleCommentService.save(bnsGroupArticleComment);
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}
}
